/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Colors;
import pojo.Images;
import pojo.Product;
import pojo.Sizes;
import services.ServiceProduct;

/**
 *
 * @author khalifaabdelaziz
 */
//ServletViewProductsRandom
public class ServletViewProductsRandom extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ServiceProduct serviceProduct = new ServiceProduct();

        try {

            List<Product> products = serviceProduct.products();
            if (products != null) {
                String html = "<h3>Featured Products</h3>\n<ul>\n";

                for (int i = 0; i < products.size(); i++) {
                    
                        Product product = products.get(i);

                        int productId = product.getId();
                        String productName = product.getName();
                        String productBrand = product.getBrand();
                        int productPrice = product.getPrice();

                        List<Colors> productColorsList = product.getColorsList();
                        String productColors = "";
                        if (!productColorsList.isEmpty()) {
                            productColors = "Colors: ";
                            for (Colors colors : productColorsList) {
                                productColors += colors.getColor() + " ";
                            }
                        }

                        List<Sizes> productSizesList = product.getSizesList();
                        String productSizes = "";
                        if (!productSizesList.isEmpty()) {
                            productSizes = "Sizes: ";
                            for (Sizes sizes : productSizesList) {
                                productSizes += sizes.getSize() + " ";
                            }
                        }

                        List<Images> productImagesList = product.getImagesList();
                        String productImage = "";
                        if (!productSizesList.isEmpty()) {
                            productImage = productImagesList.get(0).getLocation();
                        }

                        html += "<li>\n"
                                + "                            <div class=\"product\">\n"
                                + "                                <a onclick=\"viewProduct(" + productId + ")\" href=\"#\">\n"
                                + "<img src=\"" + productImage + "\" alt=\"\" />\n"
                                + "                                    <span class=\"book-name\">" + productName + "</span>\n"
                                + "                                    <span class=\"author\">" + productBrand + "</span>\n"
                                + "                                    <span class=\"price\"><span class=\"low\">L.E.</span>" + productPrice + "<span class=\"high\">00</span></span>\n"
                                + "                                </a>\n"
                                + "                            </div>\n"
                                + "                        </li>";

                    

                }
                html += "</ul> ";
                out.print(html);

            }
        } finally {
            out.close();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ServiceProduct serviceProduct = new ServiceProduct();

        try {

            List<Product> products = serviceProduct.products();
            if (products != null) {
                String html = "<h3>Featured Products</h3>\n<ul>\n";

                for (int i = 0; i < products.size(); i++) {
                    if (i % 2 != 0) {
                        Product product = products.get(i);

                        int productId = product.getId();
                        String productName = product.getName();
                        String productBrand = product.getBrand();
                        int productPrice = product.getPrice();

                        List<Colors> productColorsList = product.getColorsList();
                        String productColors = "";
                        if (!productColorsList.isEmpty()) {
                            productColors = "Colors: ";
                            for (Colors colors : productColorsList) {
                                productColors += colors.getColor() + " ";
                            }
                        }

                        List<Sizes> productSizesList = product.getSizesList();
                        String productSizes = "";
                        if (!productSizesList.isEmpty()) {
                            productSizes = "Sizes: ";
                            for (Sizes sizes : productSizesList) {
                                productSizes += sizes.getSize() + " ";
                            }
                        }

                        List<Images> productImagesList = product.getImagesList();
                        String productImage = "";
                        if (!productSizesList.isEmpty()) {
                            productImage = productImagesList.get(0).getLocation();
                        }

                        html += "<li>\n"
                                + "<div class=\"product\">\n"
                                + "<a onclick=\"viewProduct(" + productId + ")\" class=\"info\">\n"
                                + "<span class=\"holder\">\n"
                                + "<img src=\"" + productImage + "\" alt=\"\" />\n"
                                + "<span class=\"book-name\">" + productName + "</span>\n"
                                + "<span class=\"author\">" + productBrand + "</span>\n"
                                + "<span class=\"description\">\n"
                                + productSizes + "\n"
                                + "<br/>\n"
                                + productColors + "\n"
                                + "<br/></span>\n"
                                + "</span>\n"
                                + "</a>\n"
                                + "<a class=\"buy-btn\">VIEW NOW <span class=\"price\"><span class=\"low\">$</span>" + productPrice + "<span class=\"high\">00</span></span></a>\n"
                                + "</div>\n"
                                + "</li>";

                    }

                }
                html += "</ul> ";
                out.print(html);

            }
        } finally {
            out.close();
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
